package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.List;
import java.util.Map;

import com.tycmc.functions.util.MapGetter;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.util.DateUtil;

public class ParamConSaveAsFileDAO {
	private IBaseDAO baseDAO;

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	/**获取参数内容保存文件的根目录*/
	public Map<String,Object> getRootSaveFile() {
		String strSQL = "select Sys_Url_IP,Sys_Url_Repath from Sys_FileURL where Sys_Url_Type = 2";
		return this.baseDAO.execQueryOne(strSQL, new Object[]{});
	}
	/**获取上次执行的时间*/
	public String[] getCSVFileJobWorkTime() {
		String strSQL = "select convert(varchar(10),PCR_StartTime,120) StartTime,convert(varchar(10),PCR_EndTime,120) endtime from [ParamConRecord]";
		Map<String,Object> time = this.baseDAO.execQueryOne(strSQL, new Object[]{});
		String[] reStr = new String[2];
		if(null!=time&&!time.isEmpty()){
			reStr[0] = MapGetter.getString(time, "StartTime");
			reStr[1] = MapGetter.getString(time, "EndTime");
		}else{
			reStr[0] = DateUtil.addDay(-31);
			reStr[1] = DateUtil.addDay(-1);
		}
		return reStr;
	}
	
	/**查询不同软件版本的设备号*/
	public List<Map<String, Object>> getVclsOfSoft(String softType) {
		StringBuffer strSQL = new StringBuffer();
		strSQL.append("select VehicleNumber,VehicleID from V_VehicleInfo where UsefulLife>=GETDATE() and Tmnl_SoftEdition<>''  ");
		strSQL.append(" and Tmnl_SoftEdition is not null and Tmnl_SoftEdition in ("+softType+")");
		return this.baseDAO.execQuery(strSQL.toString(), new Object[]{});
	}
	/**校验设备参数信息从表是否存在*/
	public boolean checkParameterTable(String str) {
		StringBuffer strSQL = new StringBuffer();
		strSQL.append("select count(1) as counts from CTY_Egn_B.dbo.SysObjects Where XType='U' and Name in ("+str+")");
		boolean flag = false;
		if(MapGetter.getString(this.getBaseDAO().execQueryOne(strSQL.toString(), new Object[]{}), "counts").equals("2")){
			flag = true;
		}
		return flag;
	}
	/**查询主表信息**/
	public List<Map<String, Object>> queryMainTableMsg(String vclID,String time, String type) {
		StringBuffer strSQL = new StringBuffer();
		if(type.toLowerCase().equals("can")){
			strSQL.append("select MsgC_ID Msg_ID,convert(varchar(19),MsgC_MsgTime,120) MsgTime,convert(varchar(19),MsgC_RcvTime,120) RcvTime,ISNULL(SysCI_Name,'') SysTI_Name from CTY_EGN_B.dbo.Msg_CAN_"+time+" mc left join CTY_Egn_B.dbo.Sys_CANIndication on SysCI_ID = mc.MsgC_SysCI_ID " +
					" where MsgC_Vcl_ID = '"+vclID+"' order by MsgC_MsgTime desc");
		}else if(type.toLowerCase().equals("uds")){
			strSQL.append("select MsgU_ID Msg_ID,convert(varchar(19),MsgU_MsgTime,120) MsgTime,convert(varchar(19),MsgU_RcvTime,120) RcvTime,ISNULL(SysCI_Name,'') SysTI_Name from CTY_EGN_B.dbo.Msg_UDS_"+time+" mu left join CTY_Egn_B.dbo.Sys_CANIndication on SysCI_ID = mu.MsgU_SysTI_ID " +
					" where MsgU_Vcl_ID = '"+vclID+"' order by MsgU_MsgTime desc");
		}else if(type.toLowerCase().equals("pgn")){
			strSQL.append("select MsgP_ID Msg_ID,convert(varchar(19),MsgP_MsgTime,120) MsgTime,convert(varchar(19),MsgP_RcvTime,120) RcvTime,ISNULL(SysCI_Name,'') SysTI_Name from CTY_EGN_B.dbo.Msg_PGN_"+time+" mp left join CTY_Egn_B.dbo.Sys_CANIndication on SysCI_ID = mp.MsgP_SysTI_ID " +
					" where MsgP_Vcl_ID = '"+vclID+"' order by MsgP_MsgTime desc");
		}
		return this.getBaseDAO().execQuery(strSQL.toString(), new Object[]{});
	}
	/**查询从表信息**/
	public List<Map<String, Object>> queryContentTableMsg(String vclID,String time, String type) {
		StringBuffer strSQL = new StringBuffer();
		if(type.toLowerCase().equals("can")){
			strSQL.append("select MsgCCo_MsgC_ID Msg_ID,MsgCCo_SysCP_ID dicID,MsgCCo_ParmContent paramValue from CTY_Egn_B.dbo.Msg_CAN_Content_"+time+" where 1=1  ");
			strSQL.append("and exists (select 1 from CTY_Egn_B.dbo.Msg_CAN_"+time+" where MsgC_Vcl_ID = '"+vclID+"' and MsgCCo_MsgC_ID = MsgC_ID)");
		}else if(type.toLowerCase().equals("uds")){
			strSQL.append("select MsgUC_MsgU_ID Msg_ID,SysU_ID dicID,MsgUC_UDSContent paramValue from CTY_Egn_B.dbo.Msg_UDS_Content_"+time+ " inner join  CTY_EGN_B.dbo.Sys_UDS on SysU_UDSID = MsgUC_UDSID  ");
			strSQL.append("where 1=1 and exists (select 1 from CTY_Egn_B.dbo.Msg_UDS_"+time+" where MsgU_Vcl_ID = '"+vclID+"' and MsgUC_MsgU_ID = MsgU_ID)");
		}else if(type.toLowerCase().equals("pgn")){
			strSQL.append("select MsgPC_MsgP_ID Msg_ID,MsgPC_SysPIP_ID dicID,MsgPC_PGNContent paramValue from CTY_Egn_B.dbo.Msg_PGN_Content_"+time+" where 1=1 ");
			strSQL.append("and exists (select 1 from CTY_Egn_B.dbo.Msg_PGN_"+time+" where MsgP_Vcl_ID = '"+vclID+"' and MsgPC_MsgP_ID = MsgP_ID)");
		}
		return this.getBaseDAO().execQuery(strSQL.toString(), new Object[]{});
	}
	/**查询参数字典表*/
	public List<Map<String, Object>> queryDictionaryOfParameter(String type) {
		StringBuffer strBuf = new StringBuffer();
		if(type.toLowerCase().equals("can")){
			strBuf.append(" select SysCP_ID dicID ,SysCP_Name dicName from CTY_Egn_b.dbo.Sys_CAN_Parm order by SysCP_ID");
		}else if(type.toLowerCase().equals("uds")){
			strBuf.append(" select SysU_ID dicID ,SysU_UDSName dicName from CTY_Egn_b.dbo.Sys_UDS order by SysU_ID");
		}else if(type.toLowerCase().equals("pgn")){
			strBuf.append(" select SysPIP_ID dicID,SysPIP_Name dicName from CTY_Egn_B.dbo.Sys_PGN_ID_Para order by SysPIP_ID");
		}
		return this.getBaseDAO().execQuery(strBuf.toString(), new Object[]{});
	}
	/**
	 * 更新已执行时间
	 **/
	public void updateCSVFileWorkTime(String time) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("update ParamConRecord set PCR_StartTime = '"+time+"' ,PCR_EndTime = '"+DateUtil.addDay(1)+"' ");
		this.baseDAO.execUpdate(strBuf.toString(), new Object[]{});
	}
}